package cn.qyl.joha.controller.admin;

import java.awt.datatransfer.StringSelection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.dto.User;
import cn.qyl.joha.service.UserService;
import cn.qyl.joha.service.Impl.UserServiceImpl;


@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@RequestMapping("/toLogin")
	public String toLogin(){
		return "toLogin";
	}
	
	@RequestMapping("/adminHome")
	public String adminHome(){
		return "adminHome";
	}
	
	@RequestMapping("/welcome")
	public String welcome(){
		return "welcome";
	}
	
	
	@RequestMapping("/Login")
	@ResponseBody
	public Map<String,String> Login(String userId,String pwd,
							String vcode,HttpSession session){
		//从session中获取验证码
		String Code = (String) session.getAttribute(PageConstant.AUTH_CODE);
		Map<String,String> map = new HashMap<>();
		if(Code.equalsIgnoreCase(vcode)){//验证码正确
		
			UserService userService = new UserServiceImpl();
			User user = userService.getUserLogin(userId, pwd);
			if(user!=null){//用户存在
				
				if(user.getRole() > 1){// 1:普通用户   2:管理员   3:超级管理员
					//把user存到session中
					session.setAttribute(PageConstant.SESSION_USER, user);
					map.put("status", "success");
				}else{
					map.put("status", "fail");
					map.put(PageConstant.TIP, "普通用户不能登录后台!");
					
					
				}
			}else{//用户不存在
				map.put("status", "fail");
				map.put(PageConstant.TIP, "用户名或密码错误!");
			}
		}else{//验证码不正确
			map.put("status", "fail");
			map.put(PageConstant.TIP, "验证码不正确!");
		}
		
		
		return map;
	}
	
}
